﻿using Iesi.Collections.Generic;
using HMIS.Infrastructure.Core.Repositories;

namespace HMIS.Infrastructure.Core.Authority.Services.Impl
{
    public class RoleServiceImpl : IRoleService
    {
        private readonly IRoleRepository RoleRepository;

        public RoleServiceImpl(IRoleRepository RoleRepository)
        {
            this.RoleRepository = RoleRepository;
        }

        #region IRoleService 成员

        public void SaveRole(Role role, string inheritedRoleId)
        {
            if (string.IsNullOrEmpty(inheritedRoleId) == false)
            {
                Role inheritedRole = RoleRepository.GetByIdWithMenuAndPermissions(inheritedRoleId);
                role.MenuNodes = new HashedSet<MenuNode>();
                role.ActionPermissions = new HashedSet<ActionPermission>();
                role.MenuNodes.AddAll(inheritedRole.MenuNodes);
                role.ActionPermissions.AddAll(inheritedRole.ActionPermissions);
            }

            RoleRepository.SaveOrUpdate(role);
        }

        #endregion
    }
}
